package com.hyh.fireworks.web;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hyh.fireworks.common.result.AjaxResult;
import com.hyh.fireworks.entity.Posts;
import com.hyh.fireworks.entity.Tag;
import com.hyh.fireworks.entity.User;
import com.hyh.fireworks.exception.CustomException;
import com.hyh.fireworks.mapper.PostsMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.velocity.shaded.commons.io.FilenameUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Summerday
 */
@Api(tags = "测试接口调用")
@RequestMapping("/test")
@RestController
public class TestController {

    @Resource
    PostsMapper postsMapper;

    @GetMapping("/cus")
    @ApiOperation(("测试自定义异常的接口"))
    public AjaxResult testCusTomException() {
        if (Math.random() > 0) {
            throw new CustomException("此异常为自定义异常,未设置code,默认为500");
        }
        return AjaxResult.ok();
    }


    @GetMapping("/exp")
    @ApiOperation("测试系统异常")
    public AjaxResult testException() {
        int a = 1 / 0;
        return AjaxResult.ok();
    }

    @GetMapping("/data")
    @ApiOperation("正常返回数据 data{xx:xx}")
    public AjaxResult withData() {
        Map<String, Object> map = new HashMap<>(2);
        map.put("user", new User());
        map.put("tag", new Tag());
        return AjaxResult.ok(map);
    }

    @GetMapping("/page")
    public AjaxResult pageQuery(int curr, int size) {
        Page<Posts> postsPage = postsMapper.selectPage(new Page<>(curr, size), new QueryWrapper<Posts>()
                .eq("status", 1));
        return AjaxResult.ok(postsPage);
    }


    @PostMapping("/upload")
    public AjaxResult upload(MultipartFile uploadFile) {
        String originalFilename = uploadFile.getOriginalFilename();
        String name = uploadFile.getName();
        long size = uploadFile.getSize();
        String contentType = uploadFile.getContentType();
        String prefix = FilenameUtils.getPrefix(originalFilename);
        int prefixLength = FilenameUtils.getPrefixLength(originalFilename);
        String extension = FilenameUtils.getExtension(originalFilename);
        String name1 = FilenameUtils.getName(originalFilename);
        String baseName = FilenameUtils.getBaseName(originalFilename);

        System.out.println(originalFilename);
        System.out.println(name);
        System.out.println(size);
        System.out.println(contentType);
        System.out.println(prefix);
        System.out.println(prefixLength);
        System.out.println(extension);
        System.out.println(name1);
        System.out.println(baseName);

        return AjaxResult.ok();
    }

}
